#!/usr/bin/env bash

# ----------------------------------------------------------------------
# Filename    : sop.sh
# Version     : 1.0
# Date        : 2020/05/26
# Author      : Lz
# Email       : lz843723683@gmail.com
# History     :     
#               Version 1.0, 2020/05/26
# Function    : 调用不同版本形态测试程序
# In          : Features testcase name(testcase dir name)
# Out         :        
#               0=> TPASS
#               1=> TFAIL
#               other=> TCONF
# ----------------------------------------------------------------------


## TODO : 测试前的初始化验证 
#  In   : $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Init_SOP(){
	local testdir="$1"
	# 加载ltf库函数
	local ltflibfile="${LIB_LTFLIB}"
	if [ -f "$ltflibfile" ];then
		source $ltflibfile
	else
		echo "$ltflibfile : Can't found file !"
		return 1
	fi

        # 判断是否打开SE
        SEEnable_SOP
        if [ $? -ne ${TPASS} ];then
                exit $TFAIL
        fi

	# 判断是否存在免密登录库
	local sshautofile="${LIB_SSHAUTO}"
	if [ -f "$sshautofile" ];then
		source $sshautofile
	else
		echo "$sshautofile : Can't found file !"
		return $TFAIL 
	fi

	# 判断免密登录配置文件是否存在
	local sshautoconfigfile="${AUTOTEST_ROOT}/config/ssh-auto.conf"
	if [ ! -f "$sshautoconfigfile" ];then
		echo "$sshautoconfigfile : Can't found file !"
		return $TFAIL 
	fi

	# 初始化免密登录函数
	SshAuto_Init_LTFLIB "${sshautoconfigfile}"
	if [ $? -ne ${TPASS} ];then
		echo "免密登录函数初始化失败..."
		return $TFAIL
	fi

	# 配置免密登录
	SshAuto_Config_LTFLIB
	if [ $? -ne ${TPASS} ];then
		echo "免密登录设置失败..."
		return $TFAIL
	fi

	return ${TPASS}
}


## TODO ：需要默认打开SE
#
SEEnable_SOP(){
        local flag="Enforcing"

        # 使用getenforce判断
        getenforce | grep -q -i "${flag}"
        if [ $? -eq 0 ];then
                return $TPASS
        else
                echo "FAIL : SELINUX = `getenforce`"
		echo "Must be SELIUNX = enforcing"
                return $TFAIL
        fi
}


## TODO ：运行当前目录中所有的可执行文件,Run Current Dir File,前提必须进入当前目录
#   In	：
#	  $1 => 目录名
#  Out  ： 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
RunCurDirFile_SOP(){
	local testdir="$1"
        # 测试文件路径
        local testfile=""
	local flag="${TPASS}"
	local ret="${TPASS}"
	
        # 测试文件，通过数组保存
        local testfilelist=($(find ./ -maxdepth 1 -type f | sort))

        for testfile in ${testfilelist[*]}
        do
                [[ "$testfile" =~ "readme"|"swp" ]] && continue
                if [ -x "$testfile" ];then
                        bash $testfile
                        flag=$?
                else
			OverallLog_LLE "${TCONF}" "${testfile#*/} : No executable permissions"
		#	printf "\t\033[1m\033[;33m%s: %s\t[ TCONF ]\033[0m\n\n" "${testdir}/${testfile#*/}" "Permission denied"

			# 禁止降级
			if [ $ret -ne $TFAIL ];then
				ret=${TCONF}
			fi

                        continue
                fi

		OverallLog_LLE "${flag}" "${testdir}/${testfile#*/}"
		RetToFlag_LLE "${flag}" "${ret}"
		ret=$?

        done
	
	return $ret
}


## TODO : 执行测试
#  In   : $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Run_SOP(){
	# 测试文件目录
	local testdir="$1"
	local ret_1="${TPASS}"
	local ret_2="${TPASS}"
	
	# 运行当前目录下的程序,如果运行当前目录，一定要判断去除sop.sh脚本
	#RunCurDirFile_SOP "$(basename $(pwd))"
	#ret_1=$?

	# 进入测试目录
	cd ${testdir}
	RunCurDirFile_SOP "${testdir}"
	ret_2=$?

	# 结果过滤
	if [ $ret_1 -eq ${TFAIL} ];then
		return $ret_1
	elif [ $ret_1 -eq ${TPASS} ];then
		return $ret_2
	else
		if [ $ret_2 -eq ${TFAIL} ];then
			return $ret_2
		fi

		return $ret_1
	fi
}


## TODO	: 解析函数返回值,不为TPASS则退出
#  Exit	:
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
RetParse_SOP(){
	local ret=$?
	if [ $ret -ne ${TPASS} ];then
		Clear_SOP
		exit $ret
	fi
}



## TODO : 清理函数
Clear_SOP(){
	# 删除免密登录
	SshAuto_ClearConfig_LTFLIB
}

## TODO : Main_SOP
#  In	: $1=>testcase dir name
#  Out  : 
#         0=> TPASS
#         1=> TFAIL
#         other=> TCONF
Main_SOP(){
	local testdir=$(basename $1)
	cd $(dirname $1)

	Init_SOP ${testdir}
	RetParse_SOP

	Run_SOP ${testdir}
	RetParse_SOP

	Clear_SOP
	
	return ${TPASS}
}

Main_SOP $*
exit $?
